package com.fg.cloud.framework.shiro.realm;

import java.math.BigDecimal;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fg.cloud.common.finals.UserType;
import com.fg.cloud.common.shiro.CustomLoginToken;
import com.fg.cloud.common.shiro.ShiroUser;
import com.fg.cloud.common.shiro.UserUtils;
import com.fg.cloud.framework.redis.RedisCacheManager;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.service.agency.AgencyUserService;

/**
 * Description:经销商平台自定义shiro认证模块
 * @author around
 * @date 2017年8月15日上午11:33:46
 */
public class AgencyRealm extends AuthorizingRealm {

	private static Logger LOGGER = LoggerFactory.getLogger(AgencyRealm.class);
	
	@Autowired
    private AgencyUserService shiro_agencyUser;
	
	private RedisCacheManager cacheManager;
	
	@Override
	public String getName() {
		return UserType.AGENCY;
	}
	
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) 
			throws AuthenticationException {
		LOGGER.info("Shiro-agency登录认证");
		CustomLoginToken token = (CustomLoginToken) authcToken;
		AgencyUserVo user = shiro_agencyUser.selectUserByUserName(token.getUsername());
		
		//账号不存在
        if (user == null) {
            throw new UnknownAccountException();
        }
        //密码错误
        if (!String.valueOf(token.getPassword()).equals(user.getPassword())) {
        	throw new IncorrectCredentialsException();
        }
        //账号未启用
        if (1 != user.getStatus() || 1 != user.getAgencyStatus()) {
        	throw new DisabledAccountException();
        }
        
        ShiroUser shiroUser = new ShiroUser(user.getId(), user.getUserName(), 
        		user.getTrueName(), getName());
        setCurrentUser(user);
		return new SimpleAuthenticationInfo(shiroUser, token.getPassword(), getName());
	}
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		//校验正确的当前用户
		if (!principals.getRealmNames().contains(getName())) return null;
		
		ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
		//SimpleAuthorizationC test =  getAuthorizationCacheKey(principals);
		//Set<String> roles = Sets.newHashSet();
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.addRole(UserType.AGENCY);
		
		
		Object obj = getAvailablePrincipal(principals);
		Object key = getAuthorizationCacheKey(principals);
		
		return info;
	}
	
	private void setCurrentUser(Object user){
        UserUtils.setCurrentUser(user);
    }
	
	public static void main(String[] args) throws JsonProcessingException {
		BigDecimal a = new BigDecimal("99.215");//.setScale(2,BigDecimal.ROUND_HALF_UP);
        System.out.println(a); 
	}

	public RedisCacheManager getCacheManager() {
		return cacheManager;
	}

	public void setCacheManager(RedisCacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}
	
	

}
